package routes

import (
	"domain-system/handlers"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func SetupRoutes(r *gin.Engine, db *gorm.DB) {
	// 创建处理器
	paymentHandler := handlers.NewPaymentHandler(db)
	groupInviteHandler := handlers.NewGroupInviteHandler(db)

	// API版本分组
	v1 := r.Group("/api/v1")
	{
		// 支付相关路由
		payment := v1.Group("/payment")
		{
			// 检查支付状态 - GET /api/v1/payment/status?group_id=xxx&applicant_id=xxx
			payment.GET("/status", paymentHandler.CheckPaymentStatus)

			// 创建支付记录 - POST /api/v1/payment/create
			payment.POST("/create", paymentHandler.CreatePayment)

			// 更新支付状态（支付回调） - PUT /api/v1/payment/status
			payment.PUT("/status", paymentHandler.UpdatePaymentStatus)
		}

		// 群邀请链接路由
		groupInvites := v1.Group("/group_invites")
		{
			groupInvites.POST("", groupInviteHandler.CreateInviteLink)
		}
	}

	// 支持不存储、直接通过查询参数生成落地页
	r.GET("/invite", groupInviteHandler.ShowInviteLandingPage)

	// 健康检查
	r.GET("/health", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"status":  "ok",
			"message": "Payment system is running",
		})
	})

	// 数据库信息接口
	r.GET("/info", func(c *gin.Context) {
		// 获取数据库类型
		dbType := "unknown"
		if db != nil {
			if dialector := db.Dialector; dialector != nil {
				switch dialector.Name() {
				case "sqlite":
					dbType = "SQLite3"
				case "mysql":
					dbType = "MySQL"
				default:
					dbType = dialector.Name()
				}
			}
		}

		c.JSON(200, gin.H{
			"status":      "ok",
			"message":     "Payment system information",
			"database":    dbType,
			"version":     "1.0.0",
			"environment": "development",
		})
	})
}
